+function( $ ){

	var defaults = {
		
		//容器
		"container" : "#container",

		//子容器
		"sections" : ".section",

		//特效方式，ease-in, ease-out, linear
		'easing' : 'ease',

		//动画执行时间
		'duration' : 800,

		//是否分页显示
		'pagination' : true,

		//是否循环播放
		'loop' : false,

		//是否支持键盘翻页
		'keyboard' : true,

		'callback' : function(pageIndex){}
	};

	var win = $(window),
		container, sections;

	var canScroll = true,
		opts = {};

	var nowIndex = 0;

	var arrElement = [];

	var SP = $.fn.switchPage = function(options) {
		opts = $.extend({}, defaults, options || {});

		container = $(opts.container);
		sections = container.find(opts.sections);

		sections.each(function(){
			arrElement.push($(this));
		});

		return this.each(function(){
			if(opts.pagination){
				initPagination();
			}

			if(opts.keyboard) {
				keyDown();
			}

			touch();
		})
	};

	//重写鼠标滑动事件
	$(document).on("mousewheel DOMMouseScroll", MouseWheelHandler);

	function MouseWheelHandler(ev) {
		ev.preventDefault();
		var value = ev.originalEvent.wheelDelta || -ev.originalEvent.detail;

		if(canScroll) {
			if(value < 0) {
				moveSectionDown();
			}else {
				moveSectionUp();
			}
		}
	}

	//滚轮向下滑动
	function moveSectionDown() {
		if(nowIndex < (arrElement.length -1)){
			nowIndex++;
			scrollPage(arrElement[nowIndex]);
		}
	}

	//滚轮向上滑动
	function moveSectionUp() {
		if(nowIndex) {
			nowIndex--;
			scrollPage(arrElement[nowIndex]);
		}
	}

	//页面滚动事件
	function scrollPage(element){
		var dest = element.position();
		if(typeof dest == 'undefined') return;
		initEffects(dest, element);
	}

	//获取浏览器前缀
	function getVendorPrefix() {
		var body, i, style, transition, vendor;
		body = document.body || document.documentElement;
		style = body.style;
		transition = "transition";
		vendor = ["Moz", "Webkit", "Khtml", "O", "ms"];
		transition = transition.charAt(0).toUpperCase() + transition.substr(1);
		i = 0;
		while (i < vendor.length) {
			if (typeof style[vendor[i] + transition] === "string") {
				return vendor[i];
			}
			i++;
		}
		return false;
	}

	//渲染分页动画
	function initEffects(dest, element) {
		var transform = ["-webkit-transform","-ms-transform","-moz-transform","transform"],
            transition = ["-webkit-transition","-ms-transition","-moz-transition","transition"];

        canScroll = false;
        if(isSuportCss(transform) && isSuportCss(transition)){
            var traslate = "";
            if(opts.direction == "horizontal"){
                traslate = "-"+dest.left+"px, 0px, 0px";
            }else{
                traslate = "0px, -"+dest.top+"px, 0px";
            }
            container.css({
                "transition":"all "+opts.duration+"ms "+opts.easing,
				'transform' : "translate3d("+traslate+")"
            });
            container.on("webkitTransitionEnd msTransitionend mozTransitionend transitionend",function(){
                canScroll = true;
            });
        }else{
            var cssObj = (opts.direction == "horizontal")?{left: -dest.left}:{top: -dest.top};
            container.animate(cssObj, opts.duration, function(){
                canScroll = true;
            });
        }
        element.addClass("active").siblings().removeClass("active");
        if(opts.pagination){
            paginationHandler();
        }
	}

	//是否支持css的某个属性
    function isSuportCss(property){
        var body = $("body")[0];
        for(var i=0; i<property.length;i++){
            if(property[i] in body.style){
                return true;
            }
        }
        return false;
    }

	//初始化分页
    function initPagination(){
        var length = sections.length;
        if(length){

        }
        var pageHtml = '<ul id="pages"><li class="active"></li>';
        for(var i=1;i<length;i++){
            pageHtml += '<li></li>';
        }
        pageHtml += '</ul>';
        $("body").append(pageHtml);
    }

    //分页事件
    function paginationHandler(){
        var pages = $("#pages li");
        pages.eq(nowIndex).addClass("active").siblings().removeClass("active");
    }

    //绑定键盘事件
    function keyDown(){
        var keydownId;
        win.keydown(function(e){
            clearTimeout(keydownId);
            keydownId = setTimeout(function(){
                var keyCode = e.keyCode;
                if(keyCode == 37||keyCode == 38){
                    moveSectionUp();
                }else if(keyCode == 39||keyCode == 40){
                    moveSectionDown();
                }
            },150);
        });
    }

	//手机端划屏操作
	var viewHeight = $(window).height();
	function touch() {
		var viewHeight = $(window).height();
		var startY = 0;
		var downTop = 0;
		var dateTime = 0;

		container.on('touchstart', function (ev) {
			ev.preventDefault();
			var touchs = ev.originalEvent.changedTouches[0];
			startY = touchs.pageY;
			downTop = $(this).offset().top;
			dateTime = Date.now();
		});
		// container.on('touchmove', function (ev) {
		// 	ev.preventDefault();
		// });
        //
		container.on('touchend touchcancel', function (ev) {
			ev.preventDefault();
			var touchs = ev.originalEvent.changedTouches[0];

			if (Date.now() - dateTime < 300 && canScroll) {
				if (startY - touchs.pageY > 10) {
					if (nowIndex !== arrElement.length - 1) {
						moveSectionDown();
					}
				} else if (startY - touchs.pageY < 10) {
					if (nowIndex) {
						moveSectionUp();
					}
				}
			}


		});
	}
}( jQuery );